<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js原型链之深度认识__proto__</title>
</head>
<body>
  <ul>
    <li>除可以使用Object.create或setPrototypeOf修改对象原型外，其他还可以利用__proto__直接修改(不推荐)</li>
    <li>__proto__其本质而言就是一对属性访问器(getter和setter)，不可试图修改为非对象的其他类型</li>
    <li>可使用Object.create(null)修改对象原型，使其__proto__可修改为非对象类型(不推荐)</li>
  </ul>
  <script>
    const user = {
      show() {
        console.log(this.name)
      }
    }
    // Object.create
    const user1 = Object.create(user, { name: { value: 'Miracle'} })
    user1.show()  // Miracle
    const user2 = { name: 'Miracle' }
    // Object.setPrototypeOf
    Object.setPrototypeOf(user2, user)
    user2.show()  // Miracle
    // __proto__
    const user3 = { name: 'Miracle' }
    user3.__proto__ = user
    user3.show()  // Miracle
    // getter setter
    const user4 = { name: 'Miracle' }
    user4.__proto__ = user
    user4.__proto__ = 'Miracle'  // 无效
    console.log(user4)
    console.log(user4.__proto__)
    const protoObj = {
      obj: {},
      get proto() {
        return this.obj
      },
      set proto(obj) {
        if (obj instanceof Object) {
          this.obj = obj
        }
      }
    }
    protoObj.proto = { show() {} }
    protoObj.proto = 'Miracle'   // 无效
    console.log(protoObj.proto)
    // 试图修改原型为非对象类型
    const user5 = Object.create(null)
    console.log(user5.__proto__)  // undefined
    user5.__proto__ = 'Miracle'
    console.log(user5.__proto__)  // Miracle
  </script>
</body>
</html>